<?php
// 首页显示信息
namespace Store\Controller;
use Think\Controller;
class CommonController extends Controller {
    var $userArray  =   array();
    var $jsonArray =   array();
    var $brand_array = array();
	function _initialize(){
		if( !session('store_id') ){
            $this->assign('title','用户登录');
            $this->display('User:login2');
            die();
		}
        $menu   =   D('Menu');
        $menuArray  =   $menu->where('bclassid = 0 AND type = 2 AND ishidden = 0')->order('sort ASC')->select();
        if ($menuArray ){
        	foreach ($menuArray AS $key=>$row ){
                $menuChildrenArray  =   array();
                $menuChildrenArray  =   $menu->where(' bclassid = '.$row['id'].' AND type = 2 AND ishidden = 0')->order('sort ASC')->select();
                $menuArray[$key]['sub'] =   $menuChildrenArray;
        	}
        }
        $this->assign('menuArray',$menuArray);
        $menuArraySub  =   $menu->where(' module = "'.CONTROLLER_NAME.'" AND bclassid <> "0" AND type = 2 AND ishidden = 0')->order('sort ASC')->select();
        $this->assign('menuArraySub',$menuArraySub);
        
        //获取当前用户信息
        $store   =   D('store');
        $storeArray  =   $store->where('id = '.session('store_id') )->find();
        $this->storeArray    =   $storeArray;
        $this->assign('storeArray',$storeArray);

       
        
        //验证是否有权限查看当前栏目
		$jsonArray	=	json_decode($userArray['jsonstring'],true);
		
		$actionname	=	preg_replace('/list|add|save|delete/','',strtolower(ACTION_NAME));
		if( !($jsonArray[CONTROLLER_NAME][strtolower(ACTION_NAME)] || $jsonArray[CONTROLLER_NAME][ACTION_NAME]) && !$jsonArray[CONTROLLER_NAME][$actionname] && $userArray['isadmin'] != '1' && CONTROLLER_NAME != 'Public' && $actionname ){
			$oauthModule	=	D('BrandOauthModule');
			$oauthModuleArray	=	$oauthModule->field('id')->where(' module = "'.CONTROLLER_NAME.'" AND ( action = "'.strtolower(ACTION_NAME).'" OR action = "'.ACTION_NAME.'" OR action = "'.$actionname.'" )')->find();
			if( is_array($jsonArray[CONTROLLER_NAME]) && $oauthModuleArray ){
				foreach ( $jsonArray[CONTROLLER_NAME] AS $key=>$row ){
					urlSkip('','/'.CONTROLLER_NAME.'/'.$key);
				}
			}
			// urlSkipBack('您没有权限访问此栏目');
		}
		$this->jsonArray  = $jsonArray; 
        $this->assign('jsonArray',$jsonArray);

        $fromType	=	getFromType();
        $this->fromType	=	$fromType;
        $this->assign('fromType',$fromType);
	}
    
    function urlto($flag = true,$url = ''){
        $url    =   $url?'location.href="'.$url.'";':'history.back(0);';
        if( $flag !== false ){
            echo '<script type="text/javascript">alert("操作成功");'.$url.'</script>';
        }else{
            echo '<script type="text/javascript">alert("操作失败");'.$url.'</script>';
        }
    }
    
	function uploadPic($file,$width = '',$height = ''){
		//导入上传类
		$upload = new \Think\Upload();
		//设置上传文件大小
		$upload->maxSize = 3292200;
		//设置上传文件类型
		$upload->exts = array('jpg','gif','png','jpeg');
		//设置附件上传目录
		$upload->rootPath = '../Uploads/';
		$uploadList   =   $upload->uploadOne($file);
        $image = '/Uploads/'.$uploadList['savepath'].$uploadList['savename'];
        $name	=	$_SERVER["DOCUMENT_ROOT"].'/Uploads/'.$uploadList['savepath'].$uploadList['savename'];
        $imageArray	=	getimagesize($name);
        if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
	        $image1 = new \Think\Image(); 
			$image1->open($name);
			// 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
			$image1->thumb($width,$height,\Think\Image::IMAGE_THUMB_CENTER)->save($name);
        }
		return $image;
    }

    public function ajax_r($status = '0', $message = '', $data = '')
    {
        $datas['status'] = $status;
        $datas['message'] = $message;
        $datas['data'] = $data;
        $datas = json_encode($datas);
        die($datas);
    }



}
?>